<!DOCTYPE html>
<html lang="en" itemscope itemtype="https://schema.org/WebPage">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Vitess / Horizontal Sharding (Tutorial, manual)</title>

        <!-- Webfont -->
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
    
    <!--<link rel="shortcut icon" type="image/png" href="/favicon.png">-->

    <!-- Bootstrap -->
    <link href="/libs/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Styles -->
    <link rel="stylesheet" type="text/css" href="/css/site.css" />
    <!-- Font Awesome icons -->
    <link href="/libs/font-awesome-4.4.0/css/font-awesome.min.css"
          rel="stylesheet"
          type="text/css">
    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="/libs/bootstrap/js/bootstrap.min.js"></script>


    <!-- metadata -->
    <meta name="og:title" content="Vitess / Horizontal Sharding (Tutorial, manual)"/>
    <meta name="og:image" content="/images/vitess_logo_with_border.svg"/>
    <meta name="og:description" content="Vitess is a database clustering system for horizontal scaling of MySQL."/>

    <link rel="icon" href="/images/vitess_logo_icon_size.png" type="image/png">

    
    <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-60219601-1', 'auto');
  ga('send', 'pageview');
</script>

    
  </head>
  <body class="docs" id="top_of_page">

    <span id="toc-depth" data-toc-depth="h2,h3"></span>


    <nav id="common-nav" class="navbar navbar-fixed-top inverse">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">
        <img class="logo" src="/images/vitess_logo_with_border.svg" alt="Vitess">
      </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right" id="standard-menu-links">
        <li><a href="/overview/">Overview</a></li>
        <li><a href="/user-guide/introduction.html">Guides</a></li>
        <li><a href="/reference/vitess-api.html">Reference</a></li>
        <li><a href="http://blog.vitess.io">Blog</a></li>
        <li><a href="https://github.com/youtube/vitess"><i class="fa fa-github"></i> GitHub</a></li>
        <!-- Hide link to blog unless we have actual posts -->
        <!-- <li><a href="/blog/" title="">Blog</a></li> -->
      </ul>
      <ul class="nav nav-stacked mobile-left-nav-menu" id="collapsed-left-menu">
                <li class="submenu">
          <h4 class="arrow-r no-top-margin">Overview</h4>
          <ul style="display: none">
            <li><a href="/overview/">What is Vitess</a></li>
            <li><a href="/overview/scaling-mysql.html">Scaling MySQL with Vitess</a></li>
            <li><a href="/overview/concepts.html">Key Concepts</a></li>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Getting Started</h4>
          <ul style="display: none">
            <li style="padding-bottom: 0"><a href="/getting-started/">Run Vitess on Kubernetes</a>
              <ul style="display: block">
                <li style="padding-bottom: 0"><a href="/getting-started/docker-build.html">Custom Docker Build</a></li>
              </ul>
            </li>
            <li><a href="/getting-started/local-instance.html">Run Vitess Locally</a></li>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">User Guide</h4>
          <ul style="display: none">
            <li><a href="/user-guide/introduction.html">Introduction</a>
            <li><a href="/user-guide/client-libraries.html">Client Libraries</a>
            <li><a href="/user-guide/backup-and-restore.html">Backing Up Data</a>
            <li><a href="/user-guide/reparenting.html">Reparenting</a></li>
            <li style="padding-bottom: 0"><a href="/user-guide/schema-management.html">Schema Management</a></li>
              <ul style="display: block">
                <li style="padding-bottom: 0"><a href="/user-guide/vschema.html">VSchema Guide</a></li>
                <li style="padding-bottom: 0"><a href="/user-guide/schema-swap.html">Schema Swap (Tutorial)</a></li>
              </ul>
            <li style="padding-bottom: 0"><a href="/user-guide/sharding.html">Sharding</a>
              <ul style="display: block">
                <li><a href="/user-guide/horizontal-sharding-workflow.html">Horizontal Sharding (Tutorial, automated)</a></li>
                <li><a href="/user-guide/horizontal-sharding.html">Horizontal Sharding (Tutorial, manual)</a></li>
                <li><a href="/user-guide/sharding-kubernetes-workflow.html">Sharding in Kubernetes (Tutorial, automated)</a></li>
                <li style="padding-bottom: 0"><a href="/user-guide/sharding-kubernetes.html">Sharding in Kubernetes (Tutorial, manual)</a></li>
              </ul>
            </li>
            <li><a href="/user-guide/vitess-sequences.html">Vitess Sequences</a></li>
            <li><a href="/user-guide/mysql-server-protocol.html">MySQL Server Protocol</a></li>
            <li><a href="/user-guide/vitess-replication.html">Vitess and Replication</a></li>
            <li><a href="/user-guide/update-stream.html">Update Stream</a></li>
            <li><a href="/user-guide/row-based-replication.html">Row Based Replication</a></li>
            <li><a href="/user-guide/topology-service.html">Topology Service</a></li>
            <li><a href="/user-guide/transport-security-model.html">Transport Security Model</a></li>
            <li style="padding-bottom: 0"><a href="/user-guide/launching.html">Launching</a>
              <ul style="display: block">
                <li><a href="/user-guide/scalability-philosophy.html">Scalability Philosophy</a></li>
                <li><a href="/user-guide/production-planning.html">Production Planning</a></li>
                <li><a href="/user-guide/server-configuration.html">Server Configuration</a></li>
                <li><a href="/user-guide/twopc.html">2PC Guide</a></li>
                <li style="padding-bottom: 0"><a href="/user-guide/troubleshooting.html">Troubleshooting</a></li>
              </ul>
            </li>
            <li><a href="/user-guide/upgrading.html">Upgrading</a></li>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Reference Guides</h4>
          <ul style="display: none">
            <li><a href="/reference/vitess-api.html">Vitess API</a>
            <li><a href="/reference/vtctl.html">vtctl Commands</a>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Other Resources</h4>
          <ul style="display: none">
            <li><a href="/resources/presentations.html">Presentations</a>
            <li><a href="http://blog.vitess.io/">Blog</a>
            <li><a href="/resources/roadmap.html">Roadmap</a>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Contributing</h4>
          <ul style="display: none">
            <li><a href="/contributing/">Contributing to Vitess</a>
            <li><a href="/contributing/github-workflow.html">GitHub Workflow</a>
            <li><a href="/contributing/code-reviews.html">Code Reviews</a>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Internal</h4>
          <ul style="display: none">
            <li><a href="/internal/">Overview</a>
            <li><a href="/internal/release-instructions.html">Release Instructions</a>
            <li><a href="/internal/publish-website.html">Publish Website</a>
          </ul>
        </li>

        <div>
          <form method="get" action="/search/">
            <input id="search-form" name="q" type="text" placeholder="Search">
          </form>
        </div>

        <li><a href="https://github.com/youtube/vitess" id="collapsed-left-menu-repo-link"><i class="fa fa-github"></i> GitHub</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

    
<div id="masthead">
  <div class="container">
    <div class="row">
      <div class="col-md-9">
        <h1>Horizontal Sharding (Tutorial, manual)</h1>
      </div>
    </div>
  </div>
</div>


<div class="container">
    <div class="row scroll-margin" id="toc-content-row">
    <div class="col-md-2" id="leftCol">
      <ul class="nav nav-stacked mobile-left-nav-menu" id="sidebar">
                <li class="submenu">
          <h4 class="arrow-r no-top-margin">Overview</h4>
          <ul style="display: none">
            <li><a href="/overview/">What is Vitess</a></li>
            <li><a href="/overview/scaling-mysql.html">Scaling MySQL with Vitess</a></li>
            <li><a href="/overview/concepts.html">Key Concepts</a></li>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Getting Started</h4>
          <ul style="display: none">
            <li style="padding-bottom: 0"><a href="/getting-started/">Run Vitess on Kubernetes</a>
              <ul style="display: block">
                <li style="padding-bottom: 0"><a href="/getting-started/docker-build.html">Custom Docker Build</a></li>
              </ul>
            </li>
            <li><a href="/getting-started/local-instance.html">Run Vitess Locally</a></li>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">User Guide</h4>
          <ul style="display: none">
            <li><a href="/user-guide/introduction.html">Introduction</a>
            <li><a href="/user-guide/client-libraries.html">Client Libraries</a>
            <li><a href="/user-guide/backup-and-restore.html">Backing Up Data</a>
            <li><a href="/user-guide/reparenting.html">Reparenting</a></li>
            <li style="padding-bottom: 0"><a href="/user-guide/schema-management.html">Schema Management</a></li>
              <ul style="display: block">
                <li style="padding-bottom: 0"><a href="/user-guide/vschema.html">VSchema Guide</a></li>
                <li style="padding-bottom: 0"><a href="/user-guide/schema-swap.html">Schema Swap (Tutorial)</a></li>
              </ul>
            <li style="padding-bottom: 0"><a href="/user-guide/sharding.html">Sharding</a>
              <ul style="display: block">
                <li><a href="/user-guide/horizontal-sharding-workflow.html">Horizontal Sharding (Tutorial, automated)</a></li>
                <li><a href="/user-guide/horizontal-sharding.html">Horizontal Sharding (Tutorial, manual)</a></li>
                <li><a href="/user-guide/sharding-kubernetes-workflow.html">Sharding in Kubernetes (Tutorial, automated)</a></li>
                <li style="padding-bottom: 0"><a href="/user-guide/sharding-kubernetes.html">Sharding in Kubernetes (Tutorial, manual)</a></li>
              </ul>
            </li>
            <li><a href="/user-guide/vitess-sequences.html">Vitess Sequences</a></li>
            <li><a href="/user-guide/mysql-server-protocol.html">MySQL Server Protocol</a></li>
            <li><a href="/user-guide/vitess-replication.html">Vitess and Replication</a></li>
            <li><a href="/user-guide/update-stream.html">Update Stream</a></li>
            <li><a href="/user-guide/row-based-replication.html">Row Based Replication</a></li>
            <li><a href="/user-guide/topology-service.html">Topology Service</a></li>
            <li><a href="/user-guide/transport-security-model.html">Transport Security Model</a></li>
            <li style="padding-bottom: 0"><a href="/user-guide/launching.html">Launching</a>
              <ul style="display: block">
                <li><a href="/user-guide/scalability-philosophy.html">Scalability Philosophy</a></li>
                <li><a href="/user-guide/production-planning.html">Production Planning</a></li>
                <li><a href="/user-guide/server-configuration.html">Server Configuration</a></li>
                <li><a href="/user-guide/twopc.html">2PC Guide</a></li>
                <li style="padding-bottom: 0"><a href="/user-guide/troubleshooting.html">Troubleshooting</a></li>
              </ul>
            </li>
            <li><a href="/user-guide/upgrading.html">Upgrading</a></li>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Reference Guides</h4>
          <ul style="display: none">
            <li><a href="/reference/vitess-api.html">Vitess API</a>
            <li><a href="/reference/vtctl.html">vtctl Commands</a>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Other Resources</h4>
          <ul style="display: none">
            <li><a href="/resources/presentations.html">Presentations</a>
            <li><a href="http://blog.vitess.io/">Blog</a>
            <li><a href="/resources/roadmap.html">Roadmap</a>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Contributing</h4>
          <ul style="display: none">
            <li><a href="/contributing/">Contributing to Vitess</a>
            <li><a href="/contributing/github-workflow.html">GitHub Workflow</a>
            <li><a href="/contributing/code-reviews.html">Code Reviews</a>
          </ul>
        </li>
        <li class="submenu">
          <h4 class="arrow-r">Internal</h4>
          <ul style="display: none">
            <li><a href="/internal/">Overview</a>
            <li><a href="/internal/release-instructions.html">Release Instructions</a>
            <li><a href="/internal/publish-website.html">Publish Website</a>
          </ul>
        </li>

        <div>
          <form method="get" action="/search/">
            <input id="search-form" name="q" type="text" placeholder="Search">
          </form>
        </div>

      </ul>
    </div>
    <div class="col-md-3" id="rightCol">
      <div class="nav nav-stacked" id="tocSidebar">
        <div id="toc"></div>
      </div>
    </div>
    <div class="col-md-7" id="centerCol">
      <div id="centerTextCol">
        <p>This guide walks you through the process of sharding an existing unsharded
Vitess <a href="http://vitess.io/overview/concepts.html#keyspace">keyspace</a>.</p>

<h2 id="prerequisites">Prerequisites</h2>

<p>We begin by assuming you&#39;ve completed the
<a href="http://vitess.io/getting-started/local-instance.html">Getting Started</a> guide,
and have left the cluster running.</p>

<h2 id="overview">Overview</h2>

<p>The sample clients in the <code class="prettyprint">examples/local</code> folder use the following schema:</p>
<div class="highlight"><pre><code class="language-sql" data-lang="sql"><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">messages</span> <span class="p">(</span>
  <span class="n">page</span> <span class="nb">BIGINT</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span> <span class="n">UNSIGNED</span><span class="p">,</span>
  <span class="n">time_created_ns</span> <span class="nb">BIGINT</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span> <span class="n">UNSIGNED</span><span class="p">,</span>
  <span class="n">message</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">10000</span><span class="p">),</span>
  <span class="k">PRIMARY</span> <span class="k">KEY</span> <span class="p">(</span><span class="n">page</span><span class="p">,</span> <span class="n">time_created_ns</span><span class="p">)</span>
<span class="p">)</span> <span class="n">ENGINE</span><span class="o">=</span><span class="n">InnoDB</span>
</code></pre></div>
<p>The idea is that each page number represents a separate guestbook in a
multi-tenant app. Each guestbook page consists of a list of messages.</p>

<p>In this guide, we&#39;ll introduce sharding by page number.
That means pages will be randomly distributed across shards,
but all records for a given page are always guaranteed to be on the same shard.
In this way, we can transparently scale the database to support arbitrary growth
in the number of pages.</p>

<h2 id="configure-sharding-information">Configure sharding information</h2>

<p>The first step is to tell Vitess how we want to partition the data.
We do this by providing a VSchema definition as follows:</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span>
  <span class="nt">&quot;sharded&quot;</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span>
  <span class="nt">&quot;vindexes&quot;</span><span class="p">:</span> <span class="p">{</span>
    <span class="nt">&quot;hash&quot;</span><span class="p">:</span> <span class="p">{</span>
      <span class="nt">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;hash&quot;</span>
    <span class="p">}</span>
  <span class="p">},</span>
  <span class="nt">&quot;tables&quot;</span><span class="p">:</span> <span class="p">{</span>
    <span class="nt">&quot;messages&quot;</span><span class="p">:</span> <span class="p">{</span>
      <span class="nt">&quot;column_vindexes&quot;</span><span class="p">:</span> <span class="p">[</span>
        <span class="p">{</span>
          <span class="nt">&quot;column&quot;</span><span class="p">:</span> <span class="s2">&quot;page&quot;</span><span class="p">,</span>
          <span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;hash&quot;</span>
        <span class="p">}</span>
      <span class="p">]</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>This says that we want to shard the data by a hash of the <code class="prettyprint">page</code> column.
In other words, keep each page&#39;s messages together, but spread pages around
the shards randomly.</p>

<p>We can load this VSchema into Vitess like this:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ApplyVSchema -vschema <span class="s2">&quot;$(cat vschema.json)&quot;</span> test_keyspace
</code></pre></div>
<h2 id="bring-up-tablets-for-new-shards">Bring up tablets for new shards</h2>

<p>In the unsharded example, you started tablets for a shard
named <em>0</em> in <em>test_keyspace</em>, written as <em>test_keyspace/0</em>.
Now you&#39;ll start tablets for two additional shards,
named <em>test_keyspace/-80</em> and <em>test_keyspace/80-</em>:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./sharded-vttablet-up.sh
</code></pre></div>
<p>Since the sharding key is the page number,
this will result in half the pages going to each shard,
since <em>0x80</em> is the midpoint of the
<a href="http://vitess.io/user-guide/sharding.html#key-ranges-and-partitions">sharding key range</a>.</p>

<p>These new shards will run in parallel with the original shard during the
transition, but actual traffic will be served only by the original shard
until we tell it to switch over.</p>

<p>Check the <em>vtctld</em> web UI, or the output of <code class="prettyprint">lvtctl.sh ListAllTablets test</code>,
to see when the tablets are ready. There should be 5 tablets in each shard.</p>

<p>Once the tablets are ready, initialize replication by electing the first master
for each of the new shards:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./lvtctl.sh InitShardMaster -force test_keyspace/-80 <span class="nb">test</span>-0000000200
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh InitShardMaster -force test_keyspace/80- <span class="nb">test</span>-0000000300
</code></pre></div>
<p>Now there should be a total of 15 tablets, with one master for each shard:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ListAllTablets <span class="nb">test</span>
<span class="c">### example output:</span>
<span class="c"># test-0000000100 test_keyspace 0 master 10.64.3.4:15002 10.64.3.4:3306 []</span>
<span class="c"># ...</span>
<span class="c"># test-0000000200 test_keyspace -80 master 10.64.0.7:15002 10.64.0.7:3306 []</span>
<span class="c"># ...</span>
<span class="c"># test-0000000300 test_keyspace 80- master 10.64.0.9:15002 10.64.0.9:3306 []</span>
<span class="c"># ...</span>
</code></pre></div>
<h2 id="copy-data-from-original-shard">Copy data from original shard</h2>

<p>The new tablets start out empty, so we need to copy everything from the
original shard to the two new ones, starting with the schema:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./lvtctl.sh CopySchemaShard test_keyspace/0 test_keyspace/-80
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh CopySchemaShard test_keyspace/0 test_keyspace/80-
</code></pre></div>
<p>Next we copy the data. Since the amount of data to copy can be very large,
we use a special batch process called <em>vtworker</em> to stream the data from a
single source to multiple destinations, routing each row based on its
<em>keyspace_id</em>:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./sharded-vtworker.sh SplitClone test_keyspace/0
<span class="c">### example output:</span>
<span class="c"># I0416 02:08:59.952805       9 instance.go:115] Starting worker...</span>
<span class="c"># ...</span>
<span class="c"># State: done</span>
<span class="c"># Success:</span>
<span class="c"># messages: copy done, copied 11 rows</span>
</code></pre></div>
<p>Notice that we&#39;ve only specified the source shard, <em>test_keyspace/0</em>.
The <em>SplitClone</em> process will automatically figure out which shards to use
as the destinations based on the key range that needs to be covered.
In this case, shard <em>0</em> covers the entire range, so it identifies
<em>-80</em> and <em>80-</em> as the destination shards, since they combine to cover the
same range.</p>

<p>Next, it will pause replication on one <em>rdonly</em> (offline processing) tablet
to serve as a consistent snapshot of the data. The app can continue without
downtime, since live traffic is served by <em>replica</em> and <em>master</em> tablets,
which are unaffected. Other batch jobs will also be unaffected, since they
will be served only by the remaining, un-paused <em>rdonly</em> tablets.</p>

<h2 id="check-filtered-replication">Check filtered replication</h2>

<p>Once the copy from the paused snapshot finishes, <em>vtworker</em> turns on
<a href="http://vitess.io/user-guide/sharding.html#filtered-replication">filtered replication</a>
from the source shard to each destination shard. This allows the destination
shards to catch up on updates that have continued to flow in from the app since
the time of the snapshot.</p>

<p>When the destination shards are caught up, they will continue to replicate
new updates. You can see this by looking at the contents of each shard as
you add new messages to various pages in the Guestbook app. Shard <em>0</em> will
see all the messages, while the new shards will only see messages for pages
that live on that shard.</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh"><span class="c"># See what&#39;s on shard test_keyspace/0:</span>
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ExecuteFetchAsDba <span class="nb">test</span>-0000000100 <span class="s2">&quot;SELECT * FROM messages&quot;</span>
<span class="c"># See what&#39;s on shard test_keyspace/-80:</span>
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ExecuteFetchAsDba <span class="nb">test</span>-0000000200 <span class="s2">&quot;SELECT * FROM messages&quot;</span>
<span class="c"># See what&#39;s on shard test_keyspace/80-:</span>
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ExecuteFetchAsDba <span class="nb">test</span>-0000000300 <span class="s2">&quot;SELECT * FROM messages&quot;</span>
</code></pre></div>
<p>You can run the client script again to add some messages on various pages
and see how they get routed.</p>

<h2 id="check-copied-data-integrity">Check copied data integrity</h2>

<p>The <em>vtworker</em> batch process has another mode that will compare the source
and destination to ensure all the data is present and correct.
The following commands will run a diff for each destination shard:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./sharded-vtworker.sh SplitDiff test_keyspace/-80
vitess/examples/local<span class="nv">$ </span>./sharded-vtworker.sh SplitDiff test_keyspace/80-
</code></pre></div>
<p>If any discrepancies are found, they will be printed.
If everything is good, you should see something like this:</p>
<div class="highlight"><pre><code class="language-text" data-lang="text">I0416 02:10:56.927313      10 split_diff.go:496] Table messages checks out (4 rows processed, 1072961 qps)
</code></pre></div>
<h2 id="switch-over-to-new-shards">Switch over to new shards</h2>

<p>Now we&#39;re ready to switch over to serving from the new shards.
The <a href="http://vitess.io/reference/vtctl.html#migrateservedtypes">MigrateServedTypes</a>
command lets you do this one
<a href="http://vitess.io/overview/concepts.html#tablet">tablet type</a> at a time,
and even one <a href="http://vitess.io/overview/concepts.html#cell-data-center">cell</a>
at a time. The process can be rolled back at any point <em>until</em> the master is
switched over.</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./lvtctl.sh MigrateServedTypes test_keyspace/0 rdonly
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh MigrateServedTypes test_keyspace/0 replica
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh MigrateServedTypes test_keyspace/0 master
</code></pre></div>
<p>During the <em>master</em> migration, the original shard master will first stop
accepting updates. Then the process will wait for the new shard masters to
fully catch up on filtered replication before allowing them to begin serving.
Since filtered replication has been following along with live updates, there
should only be a few seconds of master unavailability.</p>

<p>When the master traffic is migrated, the filtered replication will be stopped.
Data updates will be visible on the new shards, but not on the original shard.
See it for yourself: Add a message to the guestbook page and then inspect
the database content:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh"><span class="c"># See what&#39;s on shard test_keyspace/0</span>
<span class="c"># (no updates visible since we migrated away from it):</span>
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ExecuteFetchAsDba <span class="nb">test</span>-0000000100 <span class="s2">&quot;SELECT * FROM messages&quot;</span>
<span class="c"># See what&#39;s on shard test_keyspace/-80:</span>
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ExecuteFetchAsDba <span class="nb">test</span>-0000000200 <span class="s2">&quot;SELECT * FROM messages&quot;</span>
<span class="c"># See what&#39;s on shard test_keyspace/80-:</span>
vitess/examples/local<span class="nv">$ </span>./lvtctl.sh ExecuteFetchAsDba <span class="nb">test</span>-0000000300 <span class="s2">&quot;SELECT * FROM messages&quot;</span>
</code></pre></div>
<h2 id="remove-original-shard">Remove original shard</h2>

<p>Now that all traffic is being served from the new shards, we can remove the
original one. To do that, we use the <code class="prettyprint">vttablet-down.sh</code> script from the
unsharded example:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./vttablet-down.sh
</code></pre></div>
<p>Then we can delete the now-empty shard:</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./lvtctl.sh DeleteShard -recursive test_keyspace/0
</code></pre></div>
<p>You should then see in the vtctld <strong>Topology</strong> page, or in the output of
<code class="prettyprint">lvtctl.sh ListAllTablets test</code> that the tablets for shard <em>0</em> are gone.</p>

<h2 id="tear-down-and-clean-up">Tear down and clean up</h2>

<p>Since you already cleaned up the tablets from the original unsharded example by
running <code class="prettyprint">./vttablet-down.sh</code>, that step has been replaced with
<code class="prettyprint">./sharded-vttablet-down.sh</code> to clean up the new sharded tablets.</p>
<div class="highlight"><pre><code class="language-sh" data-lang="sh">vitess/examples/local<span class="nv">$ </span>./vtgate-down.sh
vitess/examples/local<span class="nv">$ </span>./sharded-vttablet-down.sh
vitess/examples/local<span class="nv">$ </span>./vtctld-down.sh
vitess/examples/local<span class="nv">$ </span>./zk-down.sh
</code></pre></div>
      </div>
    </div>
  </div>

</div>

    <div class="page-spacer"></div>
    <footer role="contentinfo" id="site-footer">
  <nav role="navigation" class="menu bottom-menu">
    
    <a href="https://groups.google.com/forum/#!forum/vitess" target="_blank">Contact: vitess@googlegroups.com</a>&nbsp;&nbsp;<b>·</b>&nbsp;&nbsp;
    <a href="https://groups.google.com/forum/#!forum/vitess-announce" target="_blank">Announcements</a>&nbsp;&nbsp;<b>·</b>&nbsp;&nbsp;
    &#169; 2017 <a href="/">Vitess</a> powered by <a href="https://developers.google.com/open-source/">Google Inc</a>
  </nav>
</footer>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<!--    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>   -->
    <!-- Include all compiled plugins (below), or include individual files as needed -->
<!--
    <script src="/libs/bootstrap/js/bootstrap.min.js"></script>
-->
    <!-- Include the common site javascript -->
    <script src="/js/common.js" type="text/javascript" charset="utf-8"></script>


  </body>
</html>
